/*

    This file is part of Hometree.

    Hometree is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Hometree is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Hometree.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2011,2012 Roman Held

*/

/********************************************************************\
                          Includes
\********************************************************************/
#include <QDebug>
#include "htr/htrmonitor.h"

/********************************************************************\
                        Implementation
\********************************************************************/

/*--------------------------------------------------------------------
*/
HtrMonitor::HtrMonitor(HtrClient *client)
{
  mClient      = NULL;
  mInitialized = false;
  setClient(client);
}

/*--------------------------------------------------------------------
*/
void HtrMonitor::setClient(HtrClient *client)
{
  if (!client)
    return;

  Q_ASSERT(!mClient); // darf nur einmalig aufgerufen werden.

  mClient = client;
  bool done;
  done = connect(mClient, SIGNAL(stateChanged()),
          this, SLOT(clientState()));
  Q_ASSERT(done);
  done = connect(mClient, SIGNAL(urlIdent(const QString &, qint32,qint32)),
          this, SLOT(newUrlIdent(const QString &, qint32,qint32)));
  Q_ASSERT(done);
  done = connect(mClient, SIGNAL(monitorMsg(QDataStream &)),
          this, SLOT(newMonitorMsg(QDataStream &)));
  Q_ASSERT(done);

  clientState();
}

/*--------------------------------------------------------------------
*/
void HtrMonitor::clientState()
{
  Q_ASSERT(mClient);
  if (mClient->isReady() && !mInitialized) {
    init();
  }
  if (!mClient->isReady() && mInitialized) {
    mInitialized = false;
  }
}

/*--------------------------------------------------------------------
*/
void HtrMonitor::init()
{ 
  Q_ASSERT(mClient->isReady());
  mClient->prepareStream(Htr::Net::ClMonitorMsg);
  mClient->writeStream(); 
  mInitialized = true;
}

/*--------------------------------------------------------------------
*/
void HtrMonitor::newUrlIdent(const QString &url, qint32 ident, qint32 type)
{
  mUrlIdents.insert(ident,url);
  emit resDetected(url,type);
}

/*--------------------------------------------------------------------
*/
void HtrMonitor::newMonitorMsg(QDataStream &msg)
{
  qint32 resId;
  qint32 msgType;
  msg >> resId;
  msg >> msgType;
  switch(msgType) {
    case Htr::Net::EventMsg: handleEventMsg(resId, msg); break;
  }
}

/*--------------------------------------------------------------------
*/
void HtrMonitor::handleEventMsg(qint32 ident, QDataStream &msg)
{
  qint32 eventType;
  msg >> eventType;
  switch (eventType) {
    case Htr::Net::EvChanged: {
      QString url = mUrlIdents[ident];
      if (url.length()) {
        emit resChanged(url);
      }
    } break;
    case Htr::Net::EvRemoved: {
      QString url = mUrlIdents[ident];
      if (url.length()) {
        emit resRemoved(url);
      }
    } break;
  }  
}
